package com.example.lastwork;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

import android.app.assist.AssistStructure;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class MainActivity extends AppCompatActivity implements FourthFragment.FragmentInteraction {
    private RadioGroup rg_main;
    private String id;
    private String time1;
    private String time2;
    private Date d1;
    private Date d2;
    String TAG = "main";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SharedPreferences sp = this.getSharedPreferences("rate", MODE_PRIVATE);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
        String time = sp.getString("time","40");
        id = sp.getString("id",null);
        Log.i(TAG,"时间差"+id);
        Log.i(TAG,"时间差"+time);
        int t = Integer.parseInt(time);
        Date date = new Date(System.currentTimeMillis());
        Log.i(TAG,"时间差"+date);
        time1 = simpleDateFormat.format(date);
        time2 = simpleDateFormat.format(date);
        Log.i(TAG,"时间差"+time1);
        try {
            d1 = stringToDate(time1,"yyyy-MM-dd");
            Log.i(TAG,"时间差"+d1);
            d2 = stringToDate(time2,"yyyy-MM-dd");
            Log.i(TAG,"时间差"+d2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if(!TextUtils.isEmpty(id)&t<=30){
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, MainActivity2.class);
            intent.putExtra("id",id);
            startActivity(intent);
            finish();
        }else{

            SharedPreferences.Editor editor = sp.edit();
            editor.putString("time", String.valueOf((d1.getTime() - d2.getTime()) / (60 * 60 * 1000 * 24)));
            editor.apply();

            FragmentManager fragmentManager = getSupportFragmentManager();//开启事务
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.fragment, new FristFragment().getFristFragment());
            transaction.commit();


            rg_main = findViewById(R.id.rg_main);
            rg_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    for (int i = 0; i < rg_main.getChildCount(); i++) {
                        RadioButton rb = (RadioButton) group.getChildAt(i);
                        if (rb.isChecked()) {
                            setIndexSelected(i);
                            break;
                        }
                    }
                }
            });
        }
    }




    private void changeFragment(Fragment fragment)
    {
        FragmentManager fragmentManager = getSupportFragmentManager();//开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment , fragment);
        transaction.commit();
    }

    public void OnClick(View view){
        EditText E_title = findViewById(R.id.C1);
        EditText E_content = findViewById(R.id.C2);
        String title = E_title.getText().toString();
        String content = E_content.getText().toString();
        try {
            if(!TextUtils.isEmpty(title) && !TextUtils.isEmpty(content)) {
                Toast.makeText(MainActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(MainActivity.this, "标题或内容不能为空", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //通过index判断当前加载哪个界面
    public void setIndexSelected(int index)
    {
        switch (index)
        {
            case 0:
                changeFragment(new FristFragment().getFristFragment());
                break;
            case 1:
                changeFragment(new SecondFragment().getSecondFragment());
                break;
            case 2:
                changeFragment(new ThirdFragment().getThirdFragment());
                break;
            case 3:
                changeFragment(new FourthFragment().getFourthFragment());
                break;
            default:
                break;
        }
    }

    // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
    // HH时mm分ss秒，
    // strTime的时间格式必须要与formatType的时间格式相同
    public static Date stringToDate(String strTime, String formatType)
            throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        try{
            date = formatter.parse(strTime);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    @Override
    public void process(String str) {
        if (str != null) {
            id =str;
        }
    }
}